package org.hypnus.e.tag;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTag;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.hypnus.e.sub.EDataset;
import org.hypnus.e.sub.EField;
import org.hypnus.e.util.DataType;
import org.hypnus.e.util.TagType;
import org.hypnus.e.util.TagUtil;

public class ColumnTag extends BodyTagSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private String datatype = DataType.STRING;

	private String id;
	
	private String styleclass ;
	
	private String field ;
	
	private String dataset;
	
	private String dataoptions ;
	
	private String type ;
	
	private String dateformat ;
	
	public String getDateformat() {
		return dateformat;
	}

	public void setDateformat(String dateformat) {
		this.dateformat = dateformat;
	}

	public String getStyleclass() {
		return styleclass;
	}

	public void setStyleclass(String styleclass) {
		this.styleclass = styleclass;
	}

	public String getField() {
		return field;
	}

	public void setField(String field) {
		this.field = field;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getDataoptions() {
		return dataoptions;
	}

	public void setDataoptions(String dataoptions) {
		this.dataoptions = dataoptions;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getDataset() {
		return dataset;
	}

	public void setDataset(String dataset) {
		this.dataset = dataset;
	}

	public String getDatatype() {
		return datatype;
	}

	public void setDatatype(String datatype) {
		this.datatype = datatype;
	}

	
	

	@Override
	public int doEndTag() throws JspException {

		DataGridTag parent = (DataGridTag) findAncestorWithClass(this, DataGridTag.class)  ;
		
		EDataset dataset = TagUtil.getInstance().getEContext(pageContext).getDst(this.getDataset()) ;
		EField eField = new EField() ;
		eField.setId(this.getId()) ;
		eField.setDataset(this.getDataset()) ;
		eField.setName(this.getField()) ;
		eField.setDataType(this.getDatatype()) ;
		eField.setFormat(this.getDateformat()) ;
		dataset.getGrid(parent.getId()).addField(eField)  ;
		return BodyTag.EVAL_PAGE ;
	}

	@Override
	public int doStartTag() throws JspException {
		TagUtil tagUtil = TagUtil.getInstance();
		StringBuilder sb = TagUtil.getInstance().prepareAttribute(this, TagType.TH);
		tagUtil.write(pageContext, sb.toString());
		return BodyTag.EVAL_BODY_INCLUDE;
	}

}
